package com.base.kongjian.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.kongjian.dao.PopularitySystemDao;
import com.base.kongjian.entity.PopularitySystem;
import com.base.kongjian.entity.WorksContributeRec;
import com.base.kongjian.service.PopularitySystemService;
import com.base.kongjian.service.WorksContributeRecService;
import com.mysteel.page.Page;

@Service("popularitySystemService")
public class PopularitySystemServiceImpl implements PopularitySystemService
{
	@Autowired
	private PopularitySystemDao PopularitySystemDao;
	@Autowired
	private WorksContributeRecService worksContributeRecService;

	@Override
	public void save(PopularitySystem popularitySystem)
	{
		Assert.notNull (popularitySystem);
		
		long memberId = popularitySystem.getMemberId();
		
		PopularitySystem p  = PopularitySystemDao.getByMemberId (memberId);
		
		if(ObjectUtils.isNull (p))
		{
			PopularitySystemDao.save (popularitySystem);
		}
	}

	@Override
	public void delete(long memberId)
	{
		PopularitySystem p  = PopularitySystemDao.getByMemberId (memberId);
		if(ObjectUtils.notNull (p))
		{
			PopularitySystemDao.delete (memberId);
		}
	}

	@Override
	public void update(PopularitySystem popularitySystem)
	{
		Assert.notNull (popularitySystem);
		
		long memberId = popularitySystem.getMemberId ();
		PopularitySystem p  = PopularitySystemDao.getByMemberId (memberId);
		
		if(ObjectUtils.notNull (p))
		{
			p.setMemberId (popularitySystem.getMemberId ());
			p.setValue (popularitySystem.getValue ());
			
			PopularitySystemDao.update (p);	
		}else{
			
			save (popularitySystem);
		}
	}

	@Override
	public PopularitySystem getByMemberId(long memberId)
	{
		
		return PopularitySystemDao.getByMemberId (memberId);
	}

	/**
	 * （1次评论-一人气值） （1个点赞-1人气值），（1次转发-5人气值），（1次收藏-10人气值）
	 * //1-转发 ,2-收藏,3-点赞,4评论
	 */
	@Override
	public void addPopularityValue(long memberId, int type,int workKind,long workId)
	{
		PopularitySystem ps = getByMemberId (memberId);
		WorksContributeRec wcr = new WorksContributeRec (0L ,memberId ,workId ,workKind ,type ,0);
		int changeNum = 0;
		if(ObjectUtils.isNull (ps))
		{
			ps = new PopularitySystem ();
			
			ps.setMemberId (memberId);
			ps.setValue (0);	
		}
		if(type==1)
		{
			ps.setValue (ps.getValue ()+5);
			changeNum = 5;
		}else if(type==2)
		{
			ps.setValue (ps.getValue ()+10);
			changeNum = 10;
		}else if(type==3||type==4)
		{
			ps.setValue (ps.getValue ()+1);
			changeNum = 1;
		}
		
		worksContributeRecService.updateWCR (wcr,changeNum);
		update (ps);
	}

	@Override
	public Page<PopularitySystem> queryPopularitySystem(Map<String, Object> params, int currentPageNum, int maxPageRowCount)
	{
		Page<PopularitySystem> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = PopularitySystemDao.count(params);

		if (count > 0)
		{
			page = new Page<PopularitySystem>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> PopularitySystemIds = PopularitySystemDao.query(params);

			if (ObjectUtils.notEmpty(PopularitySystemIds))
			{
				List<PopularitySystem> PopularitySystems = new ArrayList<PopularitySystem>(PopularitySystemIds.size());

				for (long PopularitySystemId : PopularitySystemIds)
				{
					PopularitySystems.add(getByItsById(PopularitySystemId));
				}

				page.setDatas(PopularitySystems);
			}
		}

		return page;
	}

	@Override
	public PopularitySystem getByItsById(long id)
	{
		
		return PopularitySystemDao.getById (id);
	}
}
